// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.database.parts;

import org.dengues.model.database.DBColumn;
import org.dengues.model.database.DBNote;
import org.dengues.model.database.DBTable;
import org.dengues.model.database.DatabaseDiagram;
import org.dengues.model.database.ForeignKey;
import org.dengues.model.database.ForeignKeyLabel;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartFactory;

public class DBEditPartFactory implements EditPartFactory {

    public EditPart createEditPart(EditPart context, Object modelElement) {
        // get EditPart for model element
        EditPart part = getPartForElement(modelElement);
        // store model element in EditPart
        part.setModel(modelElement);
        return part;
    }

    private EditPart getPartForElement(Object modelElement) {
        if (modelElement instanceof DatabaseDiagram) {
            return new DBDiagramEditPart();
        } else if (modelElement instanceof DBTable) {
            return new DBTableEditPart();
        } else if (modelElement instanceof DBColumn) {
            return new DBColumnEditPart();
        } else if (modelElement instanceof ForeignKey) {
            return new ForeignKeyEditPart();
        } else if (modelElement instanceof DBNote) {
            return new DBNoteEditPart();
        } else if (modelElement instanceof ForeignKeyLabel) {
            return new ForeignKeyLabelEditPart();
        }
        throw new RuntimeException("Can't create part for model element: " //$NON-NLS-1$
                + ((modelElement != null) ? modelElement.getClass().getName() : "null")); //$NON-NLS-1$
    }

}
